package org.kl.bf.web.base;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.CodeTable;
import org.kl.bf.entity.basic.CodeValues;
import org.kl.bf.entity.easyui.ComboBox;
import org.kl.bf.entity.easyui.EasyuiTreeNode;
import org.kl.bf.service.basic.CodeTableService;
import org.kl.bf.service.basic.CodeValuesService;
import org.kl.bf.service.basic.DBProcessExecuteService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.utils.DateUtils;
import org.kl.bf.utils.PinYinTranslate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.web.MediaTypes;

/**
 * 公共方法类
 * 
 * @author Qken
 * @date 2014年4月30日
 */
@Controller
@RequestMapping(value = "/pub")
public class PubController extends BasicController {

	@Autowired
	private CodeValuesService codeValuesService;

	@Autowired
	private CodeTableService codeTableService;
	@Autowired
	private DBProcessExecuteService dBProcessExecuteService;

	/**
	 * 通过代码表获取树形下拉菜单
	 */
	@RequestMapping(value = "combotree/{tableCode}", method = RequestMethod.GET, produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String comboTree(@PathVariable("tableCode") String tableCode) {
		CodeTable codeTable = codeTableService.getByTableCode(tableCode);
		List<EasyuiTreeNode> plainTree = new ArrayList<EasyuiTreeNode>();
		if (codeTable != null) {
			List<CodeValues> codeValues = codeValuesService.findByTableId(codeTable.getId(), 0);// 0为取全部
			for (CodeValues cv : codeValues) {
				EasyuiTreeNode one = new EasyuiTreeNode();
				one.setId(cv.getId());
				one.setText(cv.getCodeName());
				one.setPid(cv.getPid());
				one.setIconCls(cv.getIconCls());
				plainTree.add(one);
			}
		}
		return jsonMapper.toJson(plainTree);
	}

	/**
	 * 通过代码表获取下拉菜单
	 */
	@RequestMapping(value = "combobox/{tableCode}/{level}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String combobox(@PathVariable("tableCode") String tableCode, @PathVariable("level") int level) {
		CodeTable codeTable = codeTableService.getByTableCode(tableCode);
		if (codeTable == null) {
			return "{}";
		}
		List<CodeValues> codeValues = codeValuesService.findByTableId(codeTable.getId(), level);// 1
																								// 为取根节点
																								// ，2为取子节点
		List<ComboBox> combobox = new ArrayList<ComboBox>();
		for (CodeValues cv : codeValues) {
			ComboBox one = new ComboBox();
			one.setId(cv.getId());
			one.setText(cv.getCodeName());
			one.setValue(cv.getCodeValue());
			combobox.add(one);
		}
		return jsonMapper.toJson(combobox);
	}

	/**
	 * 通过代码表获取下拉菜单
	 */
	@RequestMapping(value = "combobox/{tableCode}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String combobox(@PathVariable("tableCode") String tableCode) {
		CodeTable codeTable = codeTableService.getByTableCode(tableCode);
		if (codeTable == null) {
			return "{}";
		}
		List<CodeValues> codeValues = codeValuesService.findByTableId(codeTable.getId(), 0);
		List<ComboBox> combobox = new ArrayList<ComboBox>();
		for (CodeValues cv : codeValues) {
			ComboBox one = new ComboBox();
			one.setId(cv.getId());
			one.setText(cv.getCodeName());
			one.setValue(cv.getCodeValue());
			combobox.add(one);
		}
		return jsonMapper.toJson(combobox);
	}
	
	/**
	 * 通过代码表获取名称与值
	 */
	@RequestMapping(value = "getTableCode/{tableCode}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String getTableCode(@PathVariable("tableCode") String tableCode) {
		CodeTable codeTable = codeTableService.getByTableCode(tableCode);
		if (codeTable == null) {
			return "";
		}
		String tableCodeS="";
		List<CodeValues> codeValues = codeValuesService.findByTableId(codeTable.getId(), 0);
		for (CodeValues cv : codeValues) {
			if(StringUtils.isBlank(tableCodeS)){
				tableCodeS+=cv.getCodeName()+"|"+cv.getCodeValue();
			}else{
				tableCodeS+=","+cv.getCodeName()+"|"+cv.getCodeValue();
			}
		}
		return tableCodeS;
	}

	/**
	 * 获取单据号，自动通过数据库驱动类型
	 */
	@RequestMapping(value = "getDocNo/{djdm}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String getDocNo(@PathVariable("djdm") String djdm, HttpSession session) {
		String jdbcDriver = (String) session.getServletContext().getAttribute(BasicConstants.JDBC_DRIVER);
		if (StringUtils.isBlank(jdbcDriver)) {
			return ajaxMsg("单据号获取失败：没有配置数据库驱动器，无法判断获取单据号方式", true, null);
		}
		String docNo = "";
		if (StringUtils.equals(jdbcDriver, BasicConstants.JDBC_DRIVER_MYSQL)) {
			docNo = dBProcessExecuteService.getDocNoForMySQL(djdm);
		} else if (StringUtils.equals(jdbcDriver, BasicConstants.JDBC_DRIVER_ORACLE)) {
			docNo = dBProcessExecuteService.getDocNo(djdm);
		}
		return ajaxMsg("单据号获取成功", true, docNo);
	}

	/**
	 * 获取中文的首字母
	 */
	@RequestMapping(value = "getFirstLetter", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String getFirstLetter(@RequestParam(value = "cnStr", defaultValue = "") String cnStr) {
		return ajaxMsg("中文拼音首字母获取成功", true, PinYinTranslate.TranslateFirst(cnStr));
	}
	
	/**
	 * 设置年份下拉菜单
	 */
	@RequestMapping(value = "combobox/getYear", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String getYear(HttpServletRequest request) {
		int endYear = 2050;
		if (DateUtils.getCurrentYear() >= 2040) {
			endYear += 30;
		}
		List<ComboBox> combobox = new ArrayList<ComboBox>();
		for (int start = 2014; start <= endYear; start++) {
			ComboBox one = new ComboBox();
			one.setId(start + "");
			one.setText(start + "年");
			one.setValue(start + "");
			combobox.add(one);
		}
		return jsonMapper.toJson(combobox);
	}
	
	/**
	 * 不支持网页
	 */
	@RequestMapping(value = "noSupport", produces = MediaTypes.JAVASCRIPT_UTF_8)
	public String noSupport() {
		return "account/noSupport";
	}
}
